import {getSearch, getRateList} from "./src/main"
import express, {Request, Response} from "express";
import {check, validationResult} from "express-validator"

let app = express();

const execMiddleware = function (req: Request, res: Response, next: () => void) {
    let errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(400).json({errors: errors.array()})
    }
    next()
}

app.get("/list", async (req, res) => {
    let {page = 1} = req.query;
    res.json(await getSearch({...req.query, page: ~~page}))
})

app.get("/rate", [check('itemId').notEmpty().withMessage(`请输入itemId参数`)], execMiddleware, async (req: Request, res: Response) => {
    let {page = 1, itemId, pageSize = 20} = req.query;
    res.json(await getRateList({...req.query, page: ~~page, itemId: String(itemId), pageSize: ~~pageSize}))
})


app.listen(3000, () => {
    console.log('running http://localhost:3000')
})
